package com.five.filesync;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.five.filesync.bean.FileListRespondBean;
import com.five.filesync.bean.UploadRespondBean;
import com.google.gson.Gson;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class FileListRequest extends BaseRequest {
    private static final String TAG = "FileCacheListRequest";

    private static final boolean USE_CACHE = true;

    private String url;
    private OnNetCallback callback;
    public FileListRequest(InetAddress host, int port, String dirname, OnNetCallback callback) {
        this(host, port, dirname, null, callback);
    }
    public FileListRequest(InetAddress host, int port, String dirname, String prefix, OnNetCallback callback) {
        super(String.format("http://%s:%s/listfiles/%s%s", host.getHostAddress(), port, dirname, !TextUtils.isEmpty(prefix) ? "/" + prefix : ""), callback);
    }

    @Override
    protected Boolean doInBackground(String... fileUrl) {
        HttpResult result = doGetRequest(FileListRespondBean.class);
        Object objBean = result.getBean();
        if (objBean != null) {
            FileListRespondBean bean = (FileListRespondBean) result.getBean();
            if (bean.getStatus() != 0) {
                // ok
                Log.d(TAG, String.format("get fiel list successfully! url=%s", url));
                setExtra(bean.getList());
                return true;
            } else {
                Log.d(TAG, String.format("get fiel list failed! url=%s, msg=%s", url, bean.getErr()));
            }

            Log.e(TAG, String.format("download %s failed", url));
        }
        return false;
    }
}